import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import { Router } from '@angular/router';
import { maService } from '../../../service/ma/ma.service';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {spListService} from "../../../service/spList/spList.service";
import {poListService} from "../../../service/poList/poList.service";
import {formatDate} from "@angular/common";




@Component({
  selector: 'potempList-list',
  templateUrl: './potempList-list.component.html',
  styleUrls: ['./potempList-list.component.less']
})
export class potempListComponent implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    lifnr:"",
    elikz:"I",
    name1: '',
    ebeln: '',
    matnr:'',
    idnlf:'',
    ihrez:'',
    qtype:'I'
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.poListService.listtemp();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  downurl='';
  uploadExcelUrl = this.spListService.uploadpc();
  uploadExcelPOUrl = this.spListService.uploadpo();

  elikzs=[
    { text: '未完成', value: 'I' },
    { text: '已完成', value: 'X' },

  ];

loaddata(wqtype){
  // console.log(wqtype);
  // if(wqtype=='X'){
  //   this.q.RETPO =wqtype;
  //   this.q.elikz ="";
  //
  // }else{
  //   this.q.RETPO ="";
  //   this.q.elikz =wqtype;
  //
  // }
  this.q.qtype=wqtype;
  this.st.load(1,this.q)
}



  columns: SimpleTableColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },


    {
      title: '操作',
      buttons: [

        {
          text: 'BOM详情',
          type: 'link',
          click: (record: any) => this.router.navigate(['/srmbomList/srmbomList/listbyid/'+record.id])
        }
      ]
    } ,

    { title: '下单状态', index: 'loekz' },
    // { title: '采购组', index: 'ejgrp' },
    // { title: '是否已完成', index: 'elikz' ,render: 'elikz'},
    { title: '导入序号', index: 'ihrez' },
    { title: '采购组', index: 'ejgrp' },

    { title: '供应商', index: 'lifnr' },
    { title: '供应商名称', index: 'name1' },
    { title: '物料编码', index: 'matnr' },
    { title: '物料描述', index: 'maktx' },
    { title: '单位', index: 'meins' },
    { title: '订单数', index: 'menge' },
    { title: '备料单数量', index: 'menge1' },
    // { title: '半成品数量', index: 'menge2' },
    { title: '正常单数量', index: 'menge3' },

    // { title: '未清数', index: 'menge3' },
    { title: '齐套库存', index: 'kfhMenge' },

    // { title: '客供数', index: 'menge5' },
    { title: '原材备料采购订单', index: 'ebeln2' },
    // { title: '半成品采购订单', index: 'ebeln1' },
    { title: '无备料采购订单', index: 'ebeln' },

    // { title: '供应商物料编码', index: 'idnlf' },
    // { title: '供应商物料描述', index: 'lmaktx' },

    { title: '交货日期', index: 'eindt' },
    // { title: '净价', index: 'netpr' },
    // { title: '税码', index: 'nwskz' },
    // { title: '退货标识', index: 'RETPO' },
    { title: '行项目文本', index: 'ptext' },


    // { title: '单据类型', index: 'zzph' },
    // { title: '库存类型', index: 'zwxdkc' },
    { title: '是否备料', index: 'zsfbl' }
  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private maService: maService,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private poListService: poListService,
    private notice: NzNotificationService,
    private spListService: spListService,
    private confirmServ: NzModalService,


  ) {}
  syncing = false;
  lastDate = null;

  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
  getall(){
    if(!this.q.lifnr&&!this.q.ebeln){
      this.msg.warning("供应商编号或者采购订单号");
      return;
    }
    this.spListService.getekpo(this.q.lifnr,this.q.ebeln).pipe(
      catchError(err => { return of(err); })
    ).subscribe(ret=>{
      if (ret.ok) {
        this.msg.error("获取程序启动成功，请刷新");
        this.st.reload();
      } else {
        this.msg.error(ret.message);
      }
    });


  }


  xiadan(xiadantype){

    // if (this.checkedList.length > 0) {
    //   const ids = this.checkedList.map(v => v.id).join(",");
    //   console.log("ids*******"+ids);
    //   this.xiadan1(ids,xiadantype);
    //
    // } else {
    //   this.notice.create('error', '失败', '请选择进行操作');
    //
    // }

    if(this.q.ihrez == '' || this.q.ihrez == null){
      this.msg.warning("输入导入序号");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定下单?',
      nzOnOk: () => {
        this.xiadan1(xiadantype);
      },
    });
  }
  xiadan1(xiadantype){
    this.poListService.xiadan(this.q.ihrez,xiadantype).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if(data.ok){
        this.msg.success("下单成功");
        this.st.reload();
      }else{
        this.msg.error(data.message);
      }
    })
  }
  delxiadan(){

    // if (this.checkedList.length > 0) {
    //   const ids = this.checkedList.map(v => v.id).join(",");
    //   console.log("ids*******"+ids);
    //   this.delxiadan1(ids);
    //
    // } else {
    //   this.notice.create('error', '失败', '请选择进行操作');
    //
    // }
    //
    if(this.q.ihrez == '' || this.q.ihrez == null){
      this.msg.warning("输入导入序号");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定删除?',
      nzOnOk: () => {
        this.delxiadan1();
      },
    });
  }
  delxiadan1(){
    this.poListService.delxiadan(this.q.ihrez).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      if(data.ok){
        this.msg.success("下单成功");
        this.st.reload();
      }else{
        this.msg.error(data.message);
      }
    })
  }
  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    this.imgSuffix = this.comm.getGoodsUrl();
    this.emit.getMessage().subscribe(data => {
      if (data.text === 'RELOAD_GOODS_LIST') {
        this.st.load(1, this.q);
      }
    })
  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }

  checkboxChange(list: any[]) {
    this.checkedList = list;
  }




  packatta(){


    if (this.checkedList.length > 0) {
      const ids = this.checkedList.map(v => v.id).join(",");
      console.log("ids*******"+ids);
      this.xiadan2(ids);

    } else {
      this.notice.create('error', '失败', '请选择进行操作');

    }


  }



xiadan2(ids){
  this.poListService.jisuan(ids).pipe(
    catchError(err => {
      return of(err);
    })
  ).subscribe(data => {
    if(data.ok){
      this.msg.success("计算成功");
      this.st.reload();
      this.loading = false;

    }else{
      this.loading = false;

      this.msg.error(data.message);
    }
  })
}



  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }

  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;

  dateChange(result: Date[]): void {
    if (result.length !== 0) {
      this.q.startDate = formatDate(result[0], 'yyyy-MM-dd', 'en-US');
      this.q.endDate = formatDate(result[1], 'yyyy-MM-dd', 'en-US');
    } else {
      this.q.startDate = '';
      this.q.endDate = '';
    }
  }

  uploadpo(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      this.notice.create('success', '成功', info.file.response[0].piccontent);
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
}
